import { ref, reactive, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { getTaskDetail, getTaskLogs } from '@/api/task'
import { getLogDetail, getLogDetailList } from '@/api/log'

export default function useTaskLog() {
  const route = useRoute()
  const router = useRouter()
  const loading = ref(false)
  const detailLoading = ref(false)
  const dialogVisible = ref(false)
  const taskInfo = ref({})
  const logList = ref([])
  const logDetails = ref([])
  const currentLog = ref({})
  const total = ref(0)

  // 查询参数
  const queryParams = reactive({
    page: 1,
    size: 10
  })

  // 获取状态类型
  const getStatusType = (status) => {
    const statusMap = {
      'running': 'primary',
      'success': 'success',
      'failed': 'danger',
      'stopped': 'info',
      'waiting': 'warning'
    }
    return statusMap[status] || 'info'
  }

  // 获取状态文本
  const getStatusText = (status) => {
    const statusMap = {
      'running': '运行中',
      'success': '成功',
      'failed': '失败',
      'stopped': '已停止',
      'waiting': '等待中'
    }
    return statusMap[status] || '未知'
  }

  // 格式化持续时间
  const formatDuration = (startTime, endTime) => {
    if (!startTime || !endTime) return '-'
    
    const start = new Date(startTime)
    const end = new Date(endTime)
    const diff = end - start
    
    // 转换为秒
    const seconds = Math.floor(diff / 1000)
    
    if (seconds < 60) {
      return `${seconds}秒`
    } else if (seconds < 3600) {
      const minutes = Math.floor(seconds / 60)
      const remainingSeconds = seconds % 60
      return `${minutes}分${remainingSeconds}秒`
    } else {
      const hours = Math.floor(seconds / 3600)
      const minutes = Math.floor((seconds % 3600) / 60)
      const remainingSeconds = seconds % 60
      return `${hours}时${minutes}分${remainingSeconds}秒`
    }
  }

  // 获取任务信息
  const getTaskInfo = async () => {
    try {
      const res = await getTaskDetail(route.params.id)
      taskInfo.value = res.data
    } catch (error) {
      console.error('获取任务信息失败:', error)
    }
  }

  // 获取任务日志列表
  const getLogs = async () => {
    loading.value = true
    try {
      const res = await getTaskLogs(route.params.id, queryParams)
      logList.value = res.data
      total.value = res.total
    } catch (error) {
      console.error('获取任务日志列表失败:', error)
    } finally {
      loading.value = false
    }
  }

  // 刷新日志列表
  const refreshLogs = () => {
    getLogs()
  }

  // 查看日志详情
  const viewLogDetail = async (row) => {
    currentLog.value = row
    dialogVisible.value = true
    
    detailLoading.value = true
    try {
      const res = await getLogDetailList(row.id)
      logDetails.value = res.data
    } catch (error) {
      console.error('获取日志详情失败:', error)
    } finally {
      detailLoading.value = false
    }
  }

  // 每页数量变化
  const handleSizeChange = (size) => {
    queryParams.size = size
    getLogs()
  }

  // 当前页变化
  const handleCurrentChange = (page) => {
    queryParams.page = page
    getLogs()
  }

  return {
    route,
    router,
    loading,
    detailLoading,
    dialogVisible,
    taskInfo,
    logList,
    logDetails,
    currentLog,
    total,
    queryParams,
    getStatusType,
    getStatusText,
    formatDuration,
    getTaskInfo,
    getLogs,
    refreshLogs,
    viewLogDetail,
    handleSizeChange,
    handleCurrentChange
  }
}